package cz.data.domain.standard.controller;

import cz.data.common.base.BaseResponse;
import cz.data.common.base.SimpleDataResponse;
import org.apache.commons.lang3.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import cz.data.common.base.BaseController;
import cz.data.common.core.JsonPage;
import cz.data.common.validate.ValidationGroups;
import cz.data.domain.standard.mapstruct.DictMapper;
import cz.data.domain.standard.model.dto.DictDto;
import cz.data.domain.standard.model.entity.DictEntity;
import cz.data.domain.standard.model.query.DictQuery;
import cz.data.domain.standard.model.vo.DictVo;
import cz.data.domain.standard.service.DictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Api(tags = {"数据标准字典表"})
@RestController
@RequestMapping("/dicts")
public class DictController extends BaseController {

    @Autowired
    private DictService dictService;

    @Autowired
    private DictMapper dictMapper;

    @ApiOperation(value = "数据标准字典表-详情")
    @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "String", paramType = "path")
    @GetMapping("/{id}")
    public SimpleDataResponse<DictVo> getDictById(@PathVariable String id) {
        DictEntity dictEntity = dictService.getDictById(id);
        return new SimpleDataResponse<>(dictMapper.toVO(dictEntity));
    }

    @ApiOperation(value = "数据标准字典表-分页查询")
    @ApiImplicitParam(name = "dictQuery", value = "查询实体dictQuery", required = true, dataTypeClass = DictQuery.class)
    @GetMapping("/page")
    public SimpleDataResponse<JsonPage<DictVo>> getDictPage(DictQuery dictQuery) {
        QueryWrapper<DictEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(dictQuery.getTypeId()), "d.type_id", dictQuery.getTypeId());
        queryWrapper.like(StringUtils.isNotBlank(dictQuery.getGbCode()), "d.gb_code", dictQuery.getGbCode());
        queryWrapper.like(StringUtils.isNotBlank(dictQuery.getGbName()), "d.gb_name", dictQuery.getGbName());
        IPage<DictEntity> page = dictService.page(new Page<>(dictQuery.getPageNum(), dictQuery.getPageSize()), queryWrapper);
        List<DictVo> collect = page.getRecords().stream().map(dictMapper::toVO).collect(Collectors.toList());
        JsonPage<DictVo> jsonPage = new JsonPage<>(page.getCurrent(), page.getSize(), page.getTotal(), collect);
        return new SimpleDataResponse<>(jsonPage);
    }

    @ApiOperation(value = "数据标准字典表-新增")
    @ApiImplicitParam(name = "dict", value = "详细实体dict", required = true, dataType = "DictDto")
    @PostMapping()
    public SimpleDataResponse<DictVo> saveDict(@RequestBody @Validated({ValidationGroups.Insert.class}) DictDto dict) {
        DictEntity dictEntity = dictService.saveDict(dict);
        return new SimpleDataResponse<>(dictMapper.toVO(dictEntity));
    }

    @ApiOperation(value = "数据标准字典表-修改")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "String", paramType = "path"),
            @ApiImplicitParam(name = "dict", value = "详细实体dict", required = true, dataType = "DictDto")
    })
    @PutMapping("/{id}")
    public SimpleDataResponse<DictVo> updateDict(@PathVariable String id, @RequestBody @Validated({ValidationGroups.Update.class}) DictDto dict) {
        DictEntity dictEntity = dictService.updateDict(dict);
        return new SimpleDataResponse<>(dictMapper.toVO(dictEntity));
    }

    @ApiOperation(value = "数据标准字典表-删除")
    @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "String", paramType = "path")
    @DeleteMapping("/{id}")
    public BaseResponse deleteDictById(@PathVariable String id) {
        dictService.deleteDictById(id);
        return BaseResponse.success();
    }

    @ApiOperation(value = "数据标准字典表-批量删除")
    @ApiImplicitParam(name = "ids", value = "ID集合", required = true, dataType = "List", paramType = "path")
    @DeleteMapping("/batch/{ids}")
    public BaseResponse deleteDictBatch(@PathVariable List<String> ids) {
        dictService.deleteDictBatch(ids);
        return BaseResponse.success();
    }

    @ApiOperation(value = "数据标准字典表-刷新字典缓存")
    @GetMapping("/refresh")
    public BaseResponse refreshDict() {
        dictService.refreshDict();
        return BaseResponse.success();
    }
}
